﻿using System;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using StructureMap;

namespace EPiMVC.Templates.Web.Infrastructure
{
    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        private IContainer container;

        public StructureMapControllerFactory(IContainer container)
        {
            this.container = container;    
        }

        protected override IController GetControllerInstance(Type controllerType)
        {
            if (controllerType == null)
            {
                throw new HttpException(0x194, string.Format(CultureInfo.CurrentUICulture, "No controller found", new object[] { RequestContext.HttpContext.Request.Path }));
            }

            return (IController)container.GetInstance(controllerType);
        }
    }
}